;***************************************************************
; klib_ISRs.asm
;
; contains the 256 ISR stubs for non-system (driver) software
;
; the first argument passed to the ISR handler from the
; stub is the ISR number. this is passed via the RDI register
;
; Author: Aidan Goddard 26/1/14
;
;***************************************************************
; section containing 2 tables: one containing all of the ISR entry points
; and another containing the target functions

section .data

global SYSISR_ISRS
global SYSISR_TARGETS

SYSISR_TARGETS: times 256 dq 0

SYSISR_ISRS:
%assign i 0
%rep 256
	dq SYSISR_ %+ i
%assign i i+1
%endrep


;***************************************************************

section .text

; stub that saves registers and calls the given function
SYSISR_MAIN:
	; save trashed registers to the stack
	push 	rax
	push 	rbx
	push 	rcx
	push 	rdx
	push 	rsi
	push 	r8
	push 	r9
	push 	r10
	push 	r11
	push 	r12
	push 	r13
	push 	r14
	push 	r15
	push 	rbp

	; save base of stack & get address of target to call
	mov 	rbp, rsp
	mov 	rax, SYSISR_TARGETS
	mov 	rax, [rax + rdi * 8]
	call 	rax
	mov 	rsp, rbp

	; restore stack
	pop		rbp
	pop 	r15
	pop 	r14
	pop 	r13
	pop 	r12
	pop 	r11
	pop 	r10
	pop 	r9
	pop 	r8
	pop 	rsi
	pop 	rdx
	pop 	rcx
	pop 	rbx
	pop 	rax

	pop 	rdi

	; return
	iretq


; macro to create all of the ISR stubs

%assign i 0
%rep 256

SYSISR_ %+ i %+ :

	; save rdi before loading it with the ISR number and calling the above main function
	push 	rdi
	mov 	rdi, i
	jmp		SYSISR_MAIN

%assign i i+1
%endrep






















